home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 12 / Amiga Format AFCD12 (Apr 1997, Issue 96).iso / -in_the_mag- / html_tutorial / mas_rec2.cpp < prev    next >
C/C++ Source or Header  |  1997-01-21  |  4KB  |  138 lines

  1. // Record details about who the viewer of a web page is
  2. //
  3. // (C) M.A.Smith University of Brighton
  4. //
  5. // Permission is granted to use this code
  6. //   provided this declaration and copyright notice remains intact.
  7. //
  8. // 26 August 1995
  9.  
  10. #include "t99_type.h"
  11.  
  12. #include <iostream.h>
  13. #include <fstream.h>
  14. #include <iomanip.h>
  15.  
  16. #include <stdlib.h>
  17. #include <time.h>
  18. #include <string.h>
  19. #include <ctype.h>
  20.  
  21. #include "parse.h"
  22. #include "parse.cpp"
  23.  
  24. void write_log_entry( char[], char[] );
  25. void cgi_var_output();
  26. void gif_output( char [] );
  27. char* getenv_n( char [] );
  28.  
  29. // Parameters to the CGI program
  30. //  passed in the QUERY_STRING environment variable
  31. //
  32. //  file=mame  - Names the file to which log information is written
  33. //  page=name  - The name of the page that will be written to the log
  34. //  img=name   - Names the image file which is returned
  35. //             - If no name or unreadable an image of a . is returned
  36. //             - If this parameter is omitted then a web page of CGI
  37. //             -  variables is returned to help in debuging
  38.  
  39. // Remember all files name must be absolute or relative to the
  40. //  directory in which the CGI script is run
  41.  
  42.  
  43. int main()
  44. {
  45.   char *query_str = getenv("QUERY_STRING");
  46.   Parse list( query_str == 0 ? 
  47.           "file=mas&page=test&img=" : query_str );
  48.  
  49.   if ( list.get_item( "file" ) != NULL )
  50.   {
  51.     write_log_entry( list.get_item( "file", 1, true ),
  52.              list.get_item( "page" ) );
  53.   }
  54.   if ( list.get_item( "img" ) != NULL )
  55.   {
  56.     gif_output( list.get_item("img", 1, true) );
  57.   } else {
  58.     cgi_var_output();      // debug option
  59.   }
  60.   return 0;
  61. }
  62.  
  63.  
  64. void write_log_entry( char file[], char page[] )
  65. {
  66. }
  67.  
  68. void gif_output( char gif[] )
  69. {
  70.   unsigned char square [] = {
  71.     'G',  'I',  'F',  '8',  '9',  'a', 
  72.     0002, 0000, 0002, 0000, 0263, 0000, 0000, 0000, 0000, 0000,
  73.     0277, 0000, 0000, 0000, 0277, 0000, 0277, 0277,
  74.     0000, 0000, 0000, 0277, 0277, 0000, 0277, 0000,
  75.     0277, 0277, 0300, 0300, 0300, 0200, 0200, 0200,
  76.     0377, 0000, 0000, 0000, 0377, 0000, 0377, 0377,
  77.     0000, 0000, 0000, 0377, 0377, 0000, 0377, 0000,
  78.     0377, 0377, 0377, 0377, 0377, 0054, 0000, 0000,
  79.     0000, 0000, 0002, 0000, 0002, 0000, 0100, 0004,
  80.     0003, 0020, 0200, 0010, 0000, 0073 };
  81.  
  82.   cout << "Content-type: image/gif" << "\n" << "\n";
  83.  
  84.   if ( gif[0] != '\0' )            // Output gif file
  85.   {
  86.     ifstream in( gif );
  87.     if ( !in.fail() )              // Can read
  88.     { 
  89.       char c;
  90.       in.read( &c, 1 );
  91.       while ( !in.eof() )
  92.       {
  93.      cout.write( &c, 1 );
  94.      in.read( &c, 1 );
  95.       }
  96.       return;                      // Finish
  97.     }
  98.   }
  99.   cout.write( square, sizeof( square ) );
  100. }
  101.  
  102. // Debug option to deliver 
  103. // environment variables seen by the script
  104.  
  105. void cgi_var_output( )
  106. {
  107.   cout << "Content-type: text/html" << "\n"
  108.        << "\n" << "\n" << "<PRE>" << "\n";
  109.   cout << "AUTH_TYPE         " << getenv_n( "AUTH_TYPE" ) <<  "\n";
  110.   cout << "CONTENT_LENGTH    " << getenv_n( "CONTENT_LENGTH" ) <<  "\n";
  111.   cout << "CONTENT_TYPE      " << getenv_n( "CONTENT_TYPE" ) <<  "\n";
  112.   cout << "GATEWAY_INTERFACE " << getenv_n( "GATEWAY_INTERFACE" ) <<  "\n";
  113.   cout << "HTTP_ACCEPT       " << getenv_n( "HTTP_ACCEPT" ) <<  "\n";
  114.   cout << "HTTP_REFERER      " << getenv_n( "HTTP_REFERER" ) <<  "\n";
  115.   cout << "HTTP_USER_AGENT   " << getenv_n( "HTTP_USER_AGENT" ) <<  "\n";
  116.   cout << "PATH_INFO         " << getenv_n( "PATH_INFO" ) <<  "\n";
  117.   cout << "PATH_TRANSLATED   " << getenv_n( "PATH_TRANSLATED" ) <<  "\n";
  118.   cout << "QUERY_STRING      " << getenv_n( "QUERY_STRING" ) <<  "\n";
  119.   cout << "REMOTE_ADDR       " << getenv_n( "REMOTE_ADDR" ) <<  "\n";
  120.   cout << "REMOTE_IDENT      " << getenv_n( "REMOTE_IDENT" ) <<  "\n";
  121.   cout << "REMOTE_HOST       " << getenv_n( "REMOTE_HOST" ) <<  "\n";
  122.   cout << "REMOTE_USER       " << getenv_n( "REMOTE_USER" ) <<  "\n";
  123.   cout << "REQUEST_METHOD    " << getenv_n( "REQUEST_METHOD" ) <<  "\n";
  124.   cout << "SCRIPT_NAME       " << getenv_n( "SCRIPT_NAME" ) <<  "\n";
  125.   cout << "SERVER_NAME       " << getenv_n( "SERVER_NAME" ) <<  "\n";
  126.   cout << "SERVER_PORT       " << getenv_n( "SERVER_PORT" ) <<  "\n";
  127.   cout << "SERVER_PROTOCOL   " << getenv_n( "SERVER_PROTOCOL" ) <<  "\n";
  128.   cout << "SERVER_SOFTWARE   " << getenv_n( "SERVER_SOFTWARE" ) <<  "\n";
  129.   cout << "</PRE>" << "\n";
  130. }
  131.  
  132. char* getenv_n( char var[] )
  133. {
  134.   char *p = getenv( var );
  135.   return p == NULL ? "[]" : p;
  136. }
  137.  
  138.